package fr.cpe.j2ee.projet.ejb;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.TypedQuery;

import fr.cpe.j2ee.projet.model.Activity;
import fr.cpe.j2ee.projet.model.Employee;
import fr.cpe.j2ee.projet.model.Project;

/**
 * Session Bean implementation class ActivitySession
 */
@Stateless
@LocalBean
public class ActivitySession extends AbstractDAO<Activity>
{
	public ActivitySession()
	{
		super(Activity.class);
		// TODO Auto-generated constructor stub
	}

	public void assignEmployee(Employee emp, Activity act) throws IllegalArgumentException
	{
		if (emp != null && act != null)
		{
			Activity a = this.getById(act.getActId());
			
			if (a != null)
			{
				act.setEmployee(emp);
				this.update(act);
			}
			else
			{
				throw new IllegalArgumentException("Les param�tres n'existent pas en base");
			}
		}
		else
		{
			throw new IllegalArgumentException("Les param�tres ne peuvent �tre null");
		}
	}
	
	public Activity createActivity(Project proj, Employee emp, int actCharge, int actDay, String actDescription)
	{
		Activity act = new Activity();
		act.setProject(proj);
		act.setEmployee(emp);
		act.setActCharge(actCharge);
		act.setActImput(actDay);
		act.setActDescription(actDescription);
		
		this.insert(act);
		return act;
	}
	
	public Activity createActivity(Project proj, int actCharge, int actDay)
	{
		Activity act = new Activity();
		act.setProject(proj);
		act.setActCharge(actCharge);
		act.setActImput(actDay);
		
		this.insert(act);
		return act;
	}
	
	public void deleteActivity(Activity act) throws IllegalArgumentException
	{
		Activity a = this.getById(act.getActId());
		if (a != null && a.getEmployee() != null)
		{
			this.remove(a);
		}
		else
		{
			throw new IllegalArgumentException("L'activit� pass� en param�tre n'existe pas");
		}
	}
	
	public List<Employee> getWorkers(Project p)
    {
        List<Object> param = new ArrayList<Object>();
        List<Employee> workers = new ArrayList<Employee>(5);
        
        param.add(p);
        
        TypedQuery<Activity> tq = this.buildSelectQuery("select a from Activity a where a.project = ?", param);
        List<Activity> acts = tq.getResultList();
        
        for (Activity a : acts)
        {
                workers.add(a.getEmployee());
        }
        
        return workers;
    }
	
	public void removeEmployee(Activity act) 
			throws IllegalArgumentException
	{
		if (act != null)
		{
			Activity a = this.getById(act.getActId());
			
			if (a != null)
			{
				act.setEmployee(null);
				this.update(act);
			}
			else
			{
				throw new IllegalArgumentException("L'activit� n'existe pas en base");
			}
		}
		else
		{
			throw new IllegalArgumentException("L'activit� est null");
		}
	}
	
	public void setActivityCharge(Activity act, int charge) throws IllegalArgumentException
	{
		if (charge > 0 && act != null)
		{
			Activity a = this.getById(act.getActId());
			if (a != null)
			{
				act.setActImput(charge);
				this.update(act);
			}
			else
			{
				throw new IllegalArgumentException("L'activit� n'existe pas dans la base");
			}
		}
		else
		{
			throw new IllegalArgumentException("Valeurs incorrectes des param�tres");
		}
	}
	
	public void updateActivity(Activity act, Project proj, Employee emp, 
			int actCharge, int actDay) throws IllegalArgumentException
	{
		if (act != null)
		{
			Activity a = this.getById(act.getActId());
			if (a != null)
			{
				if (proj != null)
				{
					act.setProject(proj);
				}
				
				if (emp != null)
				{
					act.setEmployee(emp);
				}
				
				if (actCharge > 0)
				{
					act.setActCharge(actCharge);
				}
				
				if (actDay > 0)
				{
					act.setActImput(actDay);
				}
				
				this.update(act);
			}
			else
			{
				throw new IllegalArgumentException("L'activit� n'existe pas en base");
			}
		}
		else
		{
			throw new IllegalArgumentException("L'activit� pass� en param�tre ne peut �tre null");
		}
	}
}
